package com.team.teamproject.ui.classfiy;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.team.teamproject.R;
import com.team.teamproject.adapter.MyGridViewAdapter;
import com.team.teamproject.adapter.MyPagerFragmentAdapter1;
import com.team.teamproject.bean.Category_Channels;
import com.team.teamproject.utils.HttpUtils;
import com.team.teamproject.utils.ParseJson;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * 分类：攻略
 */

public class StrategyFragment extends Fragment {
    private static GridView gv1, gv2, gv3;
    private MyHandler handler;
    private ViewPager vp;
    private static MyGridViewAdapter gvAdapter;
    private LinearLayout container;
    private List<Fragment> fragmentLists;

    static class MyHandler extends Handler {
        private WeakReference<Activity> weakReference = null;

        MyHandler(Activity activity) {
            weakReference = new WeakReference<Activity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Activity activity = weakReference.get();
            if (activity != null) {
                switch (msg.what) {
                    case 1:
                        List<Category_Channels> category_channelsList = (List<Category_Channels>) msg.obj;
                        for (int i = 0; i < category_channelsList.size(); i++) {
                            //获取类别
                            String name = category_channelsList.get(i).getName();
                            //获取每种类别对应的数据
                            List<Category_Channels.ChannelsBean> channelsBeen = category_channelsList.get(i).getChannels();
                            //设置GridView的适配器
                            gvAdapter = new MyGridViewAdapter(channelsBeen, activity, 6);
                            if ("品类".equals(name)) {
                                gv1.setAdapter(gvAdapter);
                            } else if ("风格".equals(name)) {
                                gv2.setAdapter(gvAdapter);
                            } else if ("对象".equals(name)) {
                                gv3.setAdapter(gvAdapter);
                            }
                        }
                        break;
                    case 0:
                        Toast.makeText(activity, "下载数据失败", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_category_strategy, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        utilGet(MyUrlPath.STRATEGY_PATH);
//        getFragmentManager().beginTransaction()
//                .add(R.id.colum_container, new StrategyColumFragment())
//                .commit();
        initFragment();
        initVP();
    }

    private void initFragment() {
        fragmentLists = new ArrayList<>();
        StrategyColumFragment fragment = new StrategyColumFragment();
        StrategyColumFragment1 fragment1 = new StrategyColumFragment1();
        StrategyColumFragment2 fragment2 = new StrategyColumFragment2();
        StrategyColumFragment3 fragment3 = new StrategyColumFragment3();
        fragmentLists.add(fragment);
        fragmentLists.add(fragment1);
        fragmentLists.add(fragment2);
        fragmentLists.add(fragment3);

    }

    private void initVP() {
        MyPagerFragmentAdapter1 adapter = new MyPagerFragmentAdapter1(getChildFragmentManager(), fragmentLists);
        vp.setAdapter(adapter);
        // 1.设置幕后item的缓存数目
        vp.setOffscreenPageLimit(2);
        // 2.设置页与页之间的间
        vp.setPageMargin(5);
        // 3.将父类的touch事件分发至viewPgaer，否则只能滑动中间的一个view对象
        container.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return vp.dispatchTouchEvent(event);
            }
        });
    }

    /**
     * 下载json数据
     */
    private void utilGet(final String strategyPath) {
        //开启线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                //下载
                String json = HttpUtils.getHttpUtils().getJson(strategyPath);
                if (!TextUtils.isEmpty(json)) {
                    //解析
                    List<Category_Channels> category_channelsList = ParseJson.parseJsonToCategory(json);
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = category_channelsList;
                    handler.sendMessage(msg);
                } else {
                    handler.sendEmptyMessage(0);//json数据为空
                }
            }
        }).start();

    }

    /**
     * 初始化组件
     *
     * @param view
     */
    private void initView(View view) {
        gv1 = (GridView) view.findViewById(R.id.category_pinglei);
        gv2 = (GridView) view.findViewById(R.id.category_fengge);
        gv3 = (GridView) view.findViewById(R.id.category_duixiang);
        handler = new MyHandler(getActivity());
        vp = (ViewPager) view.findViewById(R.id.category_strategy);
        container = (LinearLayout) view.findViewById(R.id.strategy_container);
    }
}
